import * as shopService from '../../services/shop/shop';

export default {
  namespace: 'merchant',
  state:{
    pageRows:{},
    myMerchant:undefined,
    logoFileList:[],
    scrollLogosFileList:[],
  },
  reducers: {
    updatePageRows(state, {payload}) {
      const newstate = state;
      newstate.pageRows = payload;
      return newstate;
    },
    updateRow(state, {payload}) {
      const newstate = state;
      let index=null;
      const datas=newstate.pageRows.data;
      for(var i=0;i<datas.length;i++){
        if(datas[i].id==payload.id){
          index=i;
          break;
        }
      }
      if(index!=null){
        newstate.pageRows.data[index]=payload;
      }
      return newstate;
    },


    updateMyMerchant(state, {payload}) {
      const newstate = state;
      newstate.myMerchant = payload;
      newstate.logoFileList = payload.log==null?[]: [
        {
          url: payload.log,
          status: 'done',
          uid: 0 - payload.id,
        }
      ];
      newstate.scrollLogosFileList  = payload.scrollLogos===null?[]:payload.scrollLogos.map((o,index) => {
          return {
            url: o ,
            status: 'done',
            uid: index,
          }
        }
      );
      return newstate;
    },
    updateLogoFileList(state, {payload}) {
      const newstate = state;
      newstate.logoFileList = payload;
      return newstate;
    },
    updateScrollLogosFileList(state, {payload}) {
      const newstate = state;
      newstate.scrollLogosFileList = payload;
      return newstate;
    },
    updateLotLat(state, {payload}) {
      state.myMerchant={...state.myMerchant,...payload};
      return state;
    },
    updateContext(state, {payload}) {
      return {...state.myMerchant,...payload};
    },
  },
  effects: {
    *getPageMerchant({payload}, {call, put}) {
      const {data} = yield call(shopService.getPageMerchants,payload);
      yield put({type: 'updatePageRows', payload: data});
    },
    *getMyMerchant({payload}, {call, put}) {
      const {data} = yield call(shopService.getMyMerchant);
      yield put({type: 'updateMyMerchant', payload: data});
    },
    *delete({payload}, {call,put}) {
      yield call(shopService.deleteMerchants,payload);
      yield put({type: 'getPageMerchant', payload: payload.params});
    },
    *reback({payload}, {call,put}) {
      yield call(shopService.rebackMerchants,payload);
      yield put({type: 'getPageMerchant', payload: payload.params});
    },
    *add({payload}, {call,put}) {
      yield call(shopService.addMerchantMerchants,payload);
      yield put({type: 'getPageMerchant',payload:{
        pageNumber:1,
        pageSize:10
      }});
    },
    *e_update({payload}, {call,put}) {
      yield call(shopService.updateMerchantMerchants,payload);
      yield put({type: 'getMyMerchant'});
    },
    *e_chargeUpdate({payload}, {call,put}) {
      yield call(shopService.chargeUpdate,payload.chargeInfo);
      yield put({type: 'getPageMerchant', payload: payload.params});
    },
    *e_updateLogoFileList({payload}, {put}) {
      yield put({type: 'updateLogoFileList', payload: payload});
    },
    *e_updateScrollLogosFileList({payload}, {put}) {
      yield put({type: 'updateScrollLogosFileList', payload: payload});
    },
    *e_updateLotLat({payload}, {put}) {
      yield put({type: 'updateLotLat', payload: payload});
    },
    *e_updateContext({payload}, {put}) {
      yield put({type: 'updateContext', payload: payload});
    },
  },
  subscriptions: {

  },

}
